package com.wondersgroup.insurance.dnuilearn.views;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/5/9.
 */

public class FlowLayout extends ViewGroup {

    List<List<View>> mViewLineList = new ArrayList<>();
    List<Integer> mLineHeights = new ArrayList<>();
    public FlowLayout(Context context) {
        this(context,null);
    }

    public FlowLayout(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }



    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int widthSpec = MeasureSpec.getSize(widthMeasureSpec);
        int heightSpec = MeasureSpec.getSize(heightMeasureSpec);

        int widthMeasure = 0;
        int heightMeasure = 0;
        int iCurLineW = 0, iCurLineH=0;
//        if (widthMode == MeasureSpec.EXACTLY && heightMode == MeasureSpec.EXACTLY ){
//            widthMeasure = widthSpec;
//            heightMeasure = heightSpec;
//            setMeasuredDimension(widthMeasure,heightMeasure);
//        }else {
            int childCount = getChildCount();
            int childWidth  = 0, childHeight =0;

            List<View> curLine= new ArrayList<>();
            mViewLineList.add(curLine);
            mLineHeights.add(0);
            for (int i=0;i<childCount;i++){
                View child =  getChildAt(i);
                measureChild(child,widthMeasureSpec,heightMeasureSpec);
                MarginLayoutParams marginLayoutParams = (MarginLayoutParams) child.getLayoutParams();
                childWidth = child.getMeasuredWidth() + marginLayoutParams.leftMargin + marginLayoutParams.rightMargin;
                childHeight = child.getMeasuredHeight() + marginLayoutParams.topMargin + marginLayoutParams.bottomMargin;


                if (iCurLineW + childWidth >  widthSpec){
                    //1、记录当前行的最大宽度，高度累加
                    widthMeasure = Math.max(widthMeasure,iCurLineW);

                    heightMeasure += iCurLineH;

                    //2、将当前行的viewList添加至总的mViewsList，将行高添加至总的行高List
                    mViewLineList.add(curLine);
                    curLine = new ArrayList<>();
                    curLine.add(child);
                    mLineHeights.add(iCurLineH);




                    /**2、记录新一行的信息***/
                    //1、重新赋值新一行的宽、高
                    iCurLineW = childWidth;
                    iCurLineH = childHeight;





                }else {
                    iCurLineH = Math.max(iCurLineH, childHeight);

                    iCurLineW += childWidth;

                    curLine.add(child);

                }
                widthMeasure = Math.max(widthMeasure,iCurLineW);

                /*****3、如果正好是要换行**********/
                if(i == childCount - 1){
                    //1、记录当前行的最大宽度，高度累加
                    heightMeasure += iCurLineH;
                    //2、将当前行的viewList添加至总的mViewsList，将行高添加至总的行高List
//                    mViewLinesList.add(viewList);
                    mViewLineList.add(curLine);
//                    mLineHeights.add(iCurLineH);

                }
            }

//        }

        if (widthMode == MeasureSpec.EXACTLY && heightMode == MeasureSpec.EXACTLY ){
            widthMeasure = widthSpec;
            heightMeasure = heightSpec;
        }
        //最终目的
        Log.e("","widthMeasure = "+widthMeasure + "    heightMeasure = "+heightMeasure);


        setMeasuredDimension(widthMeasure,heightMeasure);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int left,top,right,bottom;
        int curLeft = 0,curTop =0;
        int lineCount = mViewLineList.size();

//        for (int i =0,size= mLineHeights.size(); i<size; i++){
//            Log.e("","mLineHeights ("+i+") = "+ mLineHeights.get(i));
//        }

        for (int i=0; i < lineCount;i++){
            List<View> views = mViewLineList.get(i);
            for (int j=0,size = views.size(); j<size; j++){
                View child = views.get(j);

                MarginLayoutParams marginLayoutParams = (MarginLayoutParams) child.getLayoutParams();
                left  = curLeft + marginLayoutParams.leftMargin;
                top = curTop + marginLayoutParams.topMargin;
                right = left + child.getMeasuredWidth();

                bottom = top + child.getMeasuredHeight();

//                Log.e("", " i = "+i + "    j = "+ j+ "   left = "+ left +"   top = "+ top + "   right = "+right + "   bottom = "+ bottom );

                child.layout(left,top,right,bottom);

                curLeft += child.getMeasuredWidth() + marginLayoutParams.leftMargin + marginLayoutParams.rightMargin;
            }

            curLeft = 0;
            if (i <mLineHeights.size() ) {
                curTop += mLineHeights.get(i);
            }


        }
        mViewLineList.clear();
        mLineHeights.clear();
    }
//**************************** 老司的代码**************************************
//    @Override
//    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//        // 获取父容器为FlowLayout设置的测量模式和大小
//        int iWidthMode = MeasureSpec.getMode(widthMeasureSpec);
//        int iHeightMode = MeasureSpec.getMode(heightMeasureSpec);
//        int iWidthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
//        int iHeightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
//
//
//        int measuredWith = 0;
//        int measuredHeight = 0;
//        int iCurLineW = 0;
//        int iCurLineH = 0;
//        if(iWidthMode == MeasureSpec.EXACTLY && iHeightMode == MeasureSpec.EXACTLY){
//            measuredWith = iWidthSpecSize;
//            measuredHeight = iHeightSpecSize;
//        }else{
//            int iChildWidth;
//            int iChildHeight;
//            int childCount = getChildCount();
//            List<View> viewList = new ArrayList<>();
//            for(int i = 0 ; i < childCount ; i++){
//                View childView = getChildAt(i);
//                measureChild(childView, widthMeasureSpec,heightMeasureSpec);
//                MarginLayoutParams layoutParams = (MarginLayoutParams) childView.getLayoutParams();
//                iChildWidth = childView.getMeasuredWidth() + layoutParams.leftMargin +
//                        layoutParams.rightMargin;
//                iChildHeight = childView.getMeasuredHeight() + layoutParams.topMargin +
//                        layoutParams.bottomMargin;
//
//                if(iCurLineW + iChildWidth > iWidthSpecSize){
//                    /**1、记录当前行的信息***/
//
//                    //1、记录当前行的最大宽度，高度累加
//                    measuredWith = Math.max(measuredWith,iCurLineW);
//                    measuredHeight += iCurLineH;
//                    //2、将当前行的viewList添加至总的mViewsList，将行高添加至总的行高List
//                    mViewLinesList.add(viewList);
//                    mLineHeights.add(iCurLineH);
//
//
//
//                    /**2、记录新一行的信息***/
//
//                    //1、重新赋值新一行的宽、高
//                    iCurLineW = iChildWidth;
//                    iCurLineH = iChildHeight;
//
//                    // 2、新建一行的viewlist，添加新一行的view
//                    viewList = new ArrayList<View>();
//                    viewList.add(childView);
//
//
//
//                }else{
//                    // 记录某行内的消息
//                    //1、行内宽度的叠加、高度比较
//                    iCurLineW += iChildWidth;
//                    iCurLineH = Math.max(iCurLineH, iChildHeight);
//
//                    // 2、添加至当前行的viewList中
//                    viewList.add(childView);
//                }
//
//                /*****3、如果正好是最后一行需要换行**********/
//                if(i == childCount - 1){
//                    //1、记录当前行的最大宽度，高度累加
//                    measuredWith = Math.max(measuredWith,iCurLineW);
//                    measuredHeight += iCurLineH;
//
//                    //2、将当前行的viewList添加至总的mViewsList，将行高添加至总的行高List
//                    mViewLinesList.add(viewList);
//                    mLineHeights.add(iCurLineH);
//
//                }
//            }
//
//
//        }
//        // 最终目的
//        setMeasuredDimension(measuredWith,measuredHeight);
//
//    }
//
//    @Override
//    protected void onLayout(boolean changed, int l, int t, int r, int b) {
//        int left,top,right,bottom;
//        int curTop = 0;
//        int curLeft = 0;
//        int lineCount = mViewLinesList.size();
//        for(int i = 0 ; i < lineCount ; i++) {
//            List<View> viewList = mViewLinesList.get(i);
//            int lineViewSize = viewList.size();
//            for(int j = 0; j < lineViewSize; j++){
//                View childView = viewList.get(j);
//                MarginLayoutParams layoutParams = (MarginLayoutParams) childView.getLayoutParams();
//
//                left = curLeft + layoutParams.leftMargin;
//                top = curTop + layoutParams.topMargin;
//                right = left + childView.getMeasuredWidth();
//                bottom = top + childView.getMeasuredHeight();
//                childView.layout(left,top,right,bottom);
//                curLeft += childView.getMeasuredWidth() + layoutParams.leftMargin + layoutParams.rightMargin;
//            }
//            curLeft = 0;
//            curTop += mLineHeights.get(i);
//        }
//        mViewLinesList.clear();
//        mLineHeights.clear();
//    }


    /**
     * 用来保存每行views的列表
     */
//    private List<List<View>> mViewLinesList = new ArrayList<>();
    /**
     * 用来保存行高的列表
     */
//    private List<Integer> mLineHeights = new ArrayList<>();
    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(),attrs);
    }


    public interface OnItemClickListener{
        void onItemClick (View v, int index);
    }

    public void setOnItemClickListener(final OnItemClickListener listener){
        int childCount = getChildCount();
        for(int i = 0 ; i < childCount ; i++){
            View childView = getChildAt(i);
            final int finalI = i;
            childView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    listener.onItemClick(v, finalI);
                }
            });
        }
    }
}
